#include <memory.h>
#include "defines.h"

int progress = 0;
static u8 *scr = 0;

//begin doing the splash screen
void splash_start(u8 *dest)
{
	scr = dest;
	progress = 0;
}

//end of splash screen, gui/emulator is showing
void splash_stop()
{

}

static u8 loading[16*8*12] = {
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

static u8 zeldaobj[12][16*12] = {
	{
	0,0,0,0,0,1,0,0,0,0,0,0,
	0,0,0,0,0,1,0,0,0,0,0,0,
	0,0,0,0,1,1,1,0,0,0,0,0,
	0,0,0,0,1,2,1,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,0,2,0,0,0,0,0,0,
	0,0,0,0,3,2,3,0,0,0,0,0,
	0,0,0,3,4,2,4,3,0,0,0,0,
	0,0,0,0,3,2,3,0,0,0,0,0,
	0,0,0,3,4,2,4,3,0,0,0,0,
	0,0,0,0,3,2,3,0,0,0,0,0,
	0,0,0,3,0,2,0,3,0,0,0,0},
	{
	0,0,0,0,0,7,0,0,0,0,0,0,
	0,0,0,0,0,0,7,0,0,0,0,0,
	0,0,0,0,0,0,0,7,0,0,0,0,
	0,0,0,0,0,0,0,7,0,0,0,0,
	0,0,0,0,0,0,7,0,0,0,0,0,
	0,0,6,6,6,6,0,0,0,0,0,0,
	0,6,8,8,6,6,6,0,0,0,0,0,
	6,8,7,8,6,6,6,6,0,0,0,0,
	6,8,8,6,6,6,6,6,0,0,0,0,
	6,6,6,6,6,6,6,6,0,0,0,0,
	6,6,6,6,6,6,6,6,0,0,0,0,
	0,6,6,6,6,6,6,0,0,0,0,0,
	0,0,6,6,6,6,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0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0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0}
};

//draw/update splash screen
void splash_draw()
{
	int i,j,k;
	int offset = 264*32+16;
	int frame;

	memset(scr,0,256+8*240);
	video_setpalentry(0xCC,0xFF,0xFF,0xFF);
	video_setpalentry(0,0x00,0x00,0x00);
	video_setpalentry(1,0xF0,0xA0,0x80);
	video_setpalentry(2,0x20,0xD0,0x20);
	video_setpalentry(3,0xC0,0x40,0x40);
	video_setpalentry(4,0x00,0x00,0x00);
	video_setpalentry(5,0x00,0x00,0xC0);
	video_setpalentry(6,0x00,0x00,0xC0);
	video_setpalentry(7,0xFF,0xFF,0xFF);
	video_setpalentry(8,0x80,0x80,0xFF);
	for(i=0;i<progress;i++) {
		frame = i;
		for(j=0;j<16;j++) {
			for(k=0;k<12;k++) {
				*(scr+offset+progress*14+j*264+k) = zeldaobj[frame % 7][k + j*12];
			}
		}
	}
	gui_draw_text(7 + 0x40,32,64,"nesemu v"VERSION);
	gui_draw_text(7 + 0x40,32,74,"loading...");
	video_update(scr,256+8);
}

//add one progress mark and update screen
void splash_adddot()
{
	progress++;
	splash_draw();
}
